package com.icangya.Utils.httpsRequest;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.icangya.weixin.sdk.WXPayConstants;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

/* 
 * 利用HttpClient进行post请求的工具类 
 */  
public class HttpClientUtil {  
    public String doPost(String url,Map<String,String> map,String charset){



        HttpClient httpClient = null;  
        HttpPost httpPost = null;  
        String result = null;  
        try{  
            httpClient = new SSLClient();  
            httpPost = new HttpPost(url);

            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.addHeader("Request Method","POST");
            //设置参数  
//            List<NameValuePair> list = new ArrayList<NameValuePair>();
//            Iterator iterator = map.entrySet().iterator();
//            while(iterator.hasNext()){
//                Entry<String,String> elem = (Entry<String, String>) iterator.next();
//                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
//            }
//            if(list.size() > 0){
//
//                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
//                httpPost.setEntity(entity);
//
//            }

//            StringBuffer queryBuffer = new StringBuffer();
//            for (Map.Entry<String, String> entry : map.entrySet()) {
//                queryBuffer.append(entry.getKey() + "=" + entry.getValue() + "&");
//            }
//            String queryStr = queryBuffer.substring(0, queryBuffer.length() - 1);

            Set<String> keySet = map.keySet();
            String[] keyArray = keySet.toArray(new String[keySet.size()]);
            Arrays.sort(keyArray);
            StringBuilder sb = new StringBuilder();
            for (String k : keyArray) {
                if (map.get(k).trim().length() > 0) {// 参数值为空，则不参与签名
                    sb.append(k).append("=").append(map.get(k).trim()).append("&");
                }
            }
            String queryStr = sb.substring(0, sb.length() - 1);

            System.out.println("");
            System.out.println("请求支付的参数："+queryStr);
            System.out.println("");

            if(queryStr!=null && !"".equals(queryStr) ){

                StringEntity entity = new StringEntity(queryStr);

                httpPost.setEntity(entity);

            }


            HttpResponse response = httpClient.execute(httpPost);

            if(response != null){  
                HttpEntity resEntity = response.getEntity();

                if(resEntity != null){

                    result = EntityUtils.toString(resEntity,charset);
                    System.out.println(result+"******************");
                }  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return result;  
    }  
}  